package com.stool.gramalldata.service.impl;

import com.stool.core.Classifier;
import com.stool.gramalldata.domain.GramData;
import com.stool.gramalldata.domain.GramResult;
import com.stool.gramalldata.service.DLService;
import com.stool.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DLServiceImpl implements DLService {

    @Autowired
    private Classifier classifier;

    @Override
    public GramResult computeResult(GramData data) {
        float[][][] floatData = convert(data);
        Result result = classifier.predict(floatData);
        GramResult gramResult = convertToGramResult(result);
        return gramResult;
    }

    @Override
    public List<GramResult> computeResults(List<GramData> dataList) {
        List<GramResult> gramResults = dataList.stream()
                .map(gramData -> computeResult(gramData))
                .collect(Collectors.toList());
        return gramResults;
    }

    private float[][][] convert(GramData data) {
        List<Float> rawData = data.getData();
        float[][][] floatData = new float[1][12][1];
        for (int i = 0; i < 12; i++) {
            floatData[0][i][0] = rawData.get(i);
        }
        return floatData;
    }

    private GramResult convertToGramResult(Result result) {
        GramResult gramResult = new GramResult();
        gramResult.setScore(result.getScore());
        gramResult.setLabel(result.getLabel());
        return gramResult;
    }
}
